package com.xxx.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.xxx.common.Const;
import com.xxx.common.dto.JsonResult;
import com.xxx.common.dto.Page;
import com.xxx.common.dto.Pagination;
import com.xxx.common.utils.ErrorUtil;
import com.xxx.common.utils.RegexValidateUtil;
import com.xxx.model.Dossier;
import com.xxx.model.FamilyHistory;
import com.xxx.service.DossierService;
import com.xxx.service.FamilyHistoryService;

@Controller
@RequestMapping("dossier/*")
public class DossierController {
	
	@Autowired
	private DossierService dossierService;
	@Autowired
	private FamilyHistoryService familyHistoryService;

	/**
	 * 列表 页面
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "index.html", method = RequestMethod.GET)
	public ModelAndView index(HttpServletRequest request) throws Exception{
		ModelAndView mv = new ModelAndView("dossier/index");
		String search = StringUtils.defaultString(request.getParameter("search"));
		int currentPage = NumberUtils.toInt(request.getParameter("page"), 1);
		Map<String, Object> whereMap = new HashMap<>();
		if(StringUtils.isNotBlank(search)){
			if(RegexValidateUtil.isPhone(search)){
				whereMap.put("phone", search);
			}else{
				whereMap.put("username", search);
			}
			mv.addObject("search", search);
		}
		Page<Dossier> page = dossierService.getPage(whereMap, new Pagination(currentPage));
		mv.addObject("page", page);
		return mv;
	}
	
	/**
	 * 添加/修改 页面
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "create.html", method = RequestMethod.GET)
	public ModelAndView create(HttpServletRequest request) throws Exception{
		ModelAndView mv = new ModelAndView("dossier/create");
		long id = NumberUtils.toLong(request.getParameter("id"), Const.DEFAULT_LONG_NULL);
		if(id != Const.DEFAULT_LONG_NULL){
			Dossier dossier = dossierService.getEntity(id);
			Map<String, Object> whereMap = new HashMap<>();
			whereMap.put("dossierid", id);
			List<FamilyHistory> familyHistoryList = familyHistoryService.getList(whereMap);
			mv.addObject("data", dossier);
			mv.addObject("list", familyHistoryList);
		}
		mv.addObject("statuslist", Const.getStatusList("否", "是"));
		return mv;
	}
	
	/**
	 * 保存/修改
	 * @param Dossier
	 * @param result
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "save", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	public JsonResult save(@Valid Dossier dossier, BindingResult result, HttpServletRequest request) throws Exception {
		if (result.hasErrors()) {
			return ErrorUtil.getError(result);
		}
		String familyhistory = request.getParameter("familyhistory");
		List<String> familyHistoryList = JSON.parseArray(familyhistory, String.class);
		long ret = dossierService.save(dossier, familyHistoryList);
		if(ret > 0){
			return JsonResult.ok();
		}
		return JsonResult.build(-1, "保存失败！");
	}
	
	/**
	 * 删除
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "delete", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	public JsonResult delete(HttpServletRequest request) throws Exception {
		String[] ids = request.getParameterValues("checkedid");
		if(ids == null || ids.length == 0){
			return JsonResult.build(-1, "请选择要删除的记录!");
		}
		for (String id : ids) {
			dossierService.delete(NumberUtils.toLong(id));
		}
		return JsonResult.ok();
	}
}
